<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="元素标识符说明" prop="identifierMsg">
        <el-input
          v-model="queryParams.identifierMsg"
          placeholder="请输入元素标识符说明"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="插入表名" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入插入表名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表字段名" prop="fieldName">
        <el-input
          v-model="queryParams.fieldName"
          placeholder="请输入表字段名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="false"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getPage"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="page" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="元素标识符说明" prop="identifierMsg" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="报文中标识符" prop="identifierValue"  width="150"/>
      <el-table-column label="编码格式" prop="codingFormat" width="150"/>
      <el-table-column label="整数位数" prop="integerBit" align="center"width="100"/>
      <el-table-column label="小数点位数" prop="radixPointBit" align="center"width="100"/>
      <el-table-column label="插入表名" prop="tableName" align="center" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="表字段名" prop="fieldName" align="center" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="字段单位" prop="fieldUnit" align="center" width="150"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getPage"
    />

    <!-- 添加或修改解析元素对话框 -->
    <el-dialog :visible.sync="open" :title="title" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="140px">
        <el-form-item label="元素标识符说明" prop="identifierMsg">
          <el-input v-model="form.identifierMsg" placeholder="请输入元素标识符说明" clearable
                    :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="报文中标识符" prop="identifierValue">
          <el-input v-model="form.identifierValue" placeholder="请输入报文中标识符" clearable
                    :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="编码格式" prop="codingFormat">
          <el-input v-model="form.codingFormat" placeholder="请输入编码格式，bcd(10进制)，hex(16进制)" clearable
                    :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="整数位数" prop="integerBit">
          <el-input-number v-model="form.integerBit" placeholder="请输入整数位数"></el-input-number>
        </el-form-item>
        <el-form-item label="小数点位数" prop="radixPointBit">
          <el-input-number v-model="form.radixPointBit" placeholder="请输入小数点位数"></el-input-number>
        </el-form-item>
        <el-form-item label="插入表名" prop="tableName">
          <el-input v-model="form.tableName" placeholder="请输入插入表名" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="表字段名" prop="fieldName">
          <el-input v-model="form.fieldName" placeholder="请输入表字段名" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="字段单位" prop="fieldUnit">
          <el-input v-model="form.fieldUnit" placeholder="请输入字段单位" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    page,
    getElement,
    insertElement,
    updateElement,
    delElement,
  } from "@/api/analysisconfigure/element";

  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        page: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {
          identifierMsg: undefined,
          identifierValue: undefined,
          codingFormat: '',
          integerBit: undefined,
          radixPointBit: undefined,
          tableName: undefined,
          fieldName: undefined,
          fieldUnit: undefined,
        },
        dialogType: '',
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          stcd: undefined,
          stName: undefined,
          stType: undefined,
          submitFrequency: undefined,
          valid: undefined,
          locked: undefined,
        },
        // 表单校验
        rules: {
          identifierMsg: [{
            required: true,
            message: '请输入元素标识符说明',
            trigger: 'blur'
          }],
          identifierValue: [],
          codingFormat: [{
            required: true,
            message: '请输入编码格式，bcd(10进制)，hex(16进制)',
            trigger: 'blur'
          }],
          integerBit: [{
            required: true,
            message: '请输入整数位数',
            trigger: 'blur'
          }],
          radixPointBit: [{
            required: true,
            message: '请输入小数点位数',
            trigger: 'blur'
          }],
          tableName: [],
          fieldName: [],
          fieldUnit: [],
        },
      };
    },
    created() {
      this.getPage();
    },
    methods: {
      /** 查询分页列表 */
      getPage() {
        this.loading = true;
        page(this.queryParams).then(response => {
            this.page = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          identifierMsg: undefined,
          identifierValue: undefined,
          codingFormat: '',
          integerBit: undefined,
          radixPointBit: undefined,
          tableName: undefined,
          fieldName: undefined,
          fieldUnit: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getPage();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.title = "新增解析元素信息";
        this.open = true;
        this.dialogType = "add";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getElement(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改解析元素息";
          this.dialogType = "update";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        let that = this;
        that.$refs["form"].validate(valid => {
          if (valid) {
            if ('update' === that.dialogType) {
              updateElement(that.form).then(response => {
                that.$modal.msgSuccess("修改成功");
                that.open = false;
                that.getPage();
              });
            } else {
              insertElement(that.form).then(response => {
                that.$modal.msgSuccess("新增成功");
                that.open = false;
                that.getPage();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const id = row.id || this.ids;
        this.$modal.confirm('是否确认删除？').then(function () {
          return delElement(id);
        }).then(() => {
          this.getPage();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/user/export', {
          ...this.queryParams
        }, `user_${new Date().getTime()}.xlsx`)
      },
    }
  };
</script>
